package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.DishStopFailedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Resource
    private DishMapper dishMapper;
    @Resource
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /*
    * 新增菜品
    * */
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        //把菜品信息放入菜品实体类中
        Dish dish = new Dish();
        //把传递过来的菜品信息放入菜品类中
        BeanUtils.copyProperties(dishDTO,dish);
        //菜品表插入一条数据
        dishMapper.insert(dish);
        //获取菜品主键值
        Long id = dish.getId();

        //口味表添加n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            //给每个口味添加菜品的id
            flavors.forEach(flavor->flavor.setDishId(id));//集合遍历
            //把口味信息添加到口味表中
            dishFlavorMapper.insertBatch(flavors);
        }




    }

    /*
    * 菜品分页查询
    * */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //利用mybatis分页插件查询
        //设置分页参数
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //执行查询
        Page<DishVO> result = dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(result.getTotal(),result.getResult());
    }

    /*
    * 批量删除菜品
    * */
    @Override
    @Transactional //开启事务
    public void deleteBatch(List<Long> ids) {
        //判断菜品是否能删除---是否为起售状态
        List<Dish> dishs = dishMapper.getStatusByIds(ids);
        //循环遍历，查看是否存在起售的菜品
        for(Dish dish : dishs){
            if(dish.getStatus() == StatusConstant.ENABLE){
                //提示菜品在销售
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断菜品是否能删除--是否在套餐中
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);//返回套餐id
        if(setmealIds != null && setmealIds.size() > 0){
            //提示当前菜品关联了套餐，不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //批量删除菜品
        dishMapper.deleteByIds(ids);
        //批量删除菜品对应的口味
        dishFlavorMapper.deleteByDishIds(ids);
    }

    //根据id查询菜品信息
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        DishVO dishVO = dishMapper.getByIdWithFlavor(id);
        return dishVO;
    }

    //修改菜品
    @Override
    @Transactional
    public void updateWithFlavor(DishVO dishVO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO,dish);
        //先修改菜品信息
        dishMapper.update(dish);
        //把菜品相关的口味信息删除
        List<Long> list = new ArrayList<>();
        list.add(dish.getId());
        dishFlavorMapper.deleteByDishIds(list);
        //添加新的口味信息
        List<DishFlavor> flavors = dishVO.getFlavors();
        if (flavors != null && flavors.size() > 0){
            //给每个口味添加菜品的id
            flavors.forEach(flavor->flavor.setDishId(dish.getId()));//集合遍历
            dishFlavorMapper.insertBatch(flavors);
        }


    }

    //根据菜品分类id查询菜品(查询可卖的)
    @Override
    public List<Dish> list(Long categoryId) {
        //创建对象
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        //根据菜品分类id查询菜品
        return dishMapper.list(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    //起售/停售菜品
    @Override
    @Transactional
    public void startOrStop(Integer status, Long id) {
        //确定该菜品是否在套餐中
        SetmealDish setmealDish = setmealDishMapper.getSetmaelDishByDishId(id);
        if(status == 0&&setmealDish != null){
            throw new DishStopFailedException(MessageConstant.DISH_STOP_FAILED);
        }
        //更新菜品状态
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.update(dish);
    }
}
